package com.junyuan.fxf_pad.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.junyuan.fxf_pad.R;
import com.junyuan.fxf_pad.application.MyApplication;
import com.junyuan.fxf_pad.utils.Consts;

import java.util.ArrayList;

public class MoreMusicActivity extends AppCompatActivity {


    private MyBroadCastReceiver myBroadCastReceiver;
    private ListView listView;
    ArrayAdapter adapter;
    int musicCount = 0;
    ArrayList<String> musicList;
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            adapter = new ArrayAdapter(MoreMusicActivity.this, R.layout.item_voice, musicList);
            listView.setAdapter(adapter);
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_music);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        listView = (ListView) findViewById(R.id.lv_more_music);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        if (MyApplication.instance.mBleService != null) {
            MyApplication.instance.mBleService.writeCharacteristic(MyApplication.instance.mCharacteristic, "#L1");
        }

        myBroadCastReceiver = new MyBroadCastReceiver();
        IntentFilter filter = new IntentFilter("BackData");
        registerReceiver(myBroadCastReceiver, filter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("mytag", "点击了" + position + "位置");
                String song = "";
                if (position < 10) {
                    song = "00" + (position + 1);
                } else {
                    song = "0" + (position + 1);
                }
                if (MyApplication.instance.mBleService != null) {
                    MyApplication.instance.mBleService.writeCharacteristic
                            (MyApplication.instance.mCharacteristic, song);
                    Log.d("mytag", "点击了" + song);
                }
                setResult(Consts.MUSIC_SELECTED);
                MoreMusicActivity.this.finish();
//                MyApplication.instance.backFromMoreMusic=true;

            }
        });
    }


    public class MyBroadCastReceiver extends BroadcastReceiver {


        @Override
        public void onReceive(Context context, Intent intent) {
            String backData = intent.getStringExtra("BackData");
            Log.d("mytag", "MoreMusicActivity的广播接收到：" + backData);
            try {
                musicCount = Integer.parseInt(backData);
                musicList = new ArrayList<>();
                musicList = generMusicList(musicCount);
                handler.sendEmptyMessage(0);
            } catch (NumberFormatException e) {
                Toast.makeText(MoreMusicActivity.this, "收到的数据不是数字", Toast.LENGTH_SHORT).show();
            }


        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadCastReceiver);

    }

    public ArrayList<String> generMusicList(int arg) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 1; i <= arg; i++) {
            list.add("第" + i + "首歌");
        }
        return list;
    }


}
